package com.nerve.cloudoffice.service.impl;

import java.util.Date;

import org.nerve.util.FormatUtil;

import com.nerve.cloudoffice.common.SessionUtil;
import com.nerve.cloudoffice.domain.Notify;
import com.nerve.cloudoffice.domain.dao.NotifyDao;
import com.nerve.cloudoffice.service.NotifyService;
import com.nerve.cloudoffice.service.ServiceExceptions;
import com.nerve.cloudoffice.web.model.NotifyModel;

/**
 * @project cloudOffice NotifyServiceImpl.java
 * @datetime Aug 2, 2013 3:21:18 AM
 * @description 
 *    
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
public class NotifyServiceImpl implements NotifyService{

	private NotifyDao dao;
	
	private NotifyDao getDao(){
		if(dao == null)
			dao = new NotifyDao();
		
		return dao;
	}
	
	public void delete(NotifyModel model) throws Exception {
		if(model.getId() > 0)
			getDao().delete(model.getId());
		else
			throw new RuntimeException(ServiceExceptions.ID_REQUIRED);
	}

	public void list(NotifyModel model) throws Exception {
		model.setTotalCount(getDao().countRow("From Notify"));
		
		model.setNotifyList(
				getDao().getList("From Notify order by id desc",model.getPage(), model.getPageSize())
				);
	}

	public void save(NotifyModel model) throws Exception {
		Notify notify = model.getNotify();
		FormatUtil.notNull(notify, "参数错误：Notify 对象为空！");
		
		if(notify.getTitle() == null)
			throw new RuntimeException(ServiceExceptions.NOTIFY_TITLE_REQUIRED);
		
		if(notify.getMessage() == null)
			throw new RuntimeException(ServiceExceptions.NOTIFY_MESSAGE_REQUIRED);
		
		notify.setJoinDate(new Date());
		notify.setUser(SessionUtil.getLoginUser());
		getDao().save(notify);
	}

}